﻿using AutoMapper;
using Common.Communal;
using ERP.Domain.System;
using MediatR;
using System.Api.Write.Applications.Commands.Users;
using System.Services.Users;

namespace System.Api.Write.Applications.CommandHandlers.Users
{
    /// <summary>
    /// 用户更新命令处理程序
    /// </summary>
    public class UserUpdateCommandHandler : IRequestHandler<UserUpdateCommand, ApiResult<int>>
    {
        /// <summary>
        /// 用户服务接口
        /// </summary>
        private readonly IUserService service;
        /// <summary>
        /// 映射接口
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造函数，注入用户服务接口
        /// </summary>
        /// <param name="service">用户服务接口</param>
        /// <param name="mapper">映射接口</param>
        public UserUpdateCommandHandler(IUserService service,IMapper mapper)
        {
            this.service = service;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理用户更新命令
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回结果</returns>
        public Task<ApiResult<int>> Handle(UserUpdateCommand request, CancellationToken cancellationToken)
        {
            var res = mapper.Map<User>(request);
            return service.Update(res);
        }
    }
}
